package com.huike.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huike.pojo.Charge;
import com.huike.service.ChargeService;
import com.huike.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("charge")
@ResponseBody
public class ChargeController {
    @Autowired
    private ChargeService chargeService;
    @RequestMapping("delete/{id}")
    public ResultVO deleteCharge(@PathVariable("id") Integer id){

        try{
            chargeService.deleteCharge(id);
            return new ResultVO(200,"删除成功");
        }catch (Exception e){
            return new ResultVO(300,"删除失败");
        }

    }

    @RequestMapping("getCharge")
    public ResultVO getChargeById(@RequestParam Integer id){
        Charge charge =  chargeService.getChargeById(id);
        return new ResultVO(charge);
    }

    @RequestMapping("list2")
    public ResultVO<PageInfo> getChargeAll( String chargeName,@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5")  Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Charge> charges = chargeService.getChargeAll(chargeName);
        PageInfo pageInfo = new PageInfo(charges,5);
        return new ResultVO(pageInfo);
    }

    @RequestMapping("updateAdvance")
    public ResultVO updateCharge(Charge charge){

        try{
            chargeService.updateCharge(charge);
            return new ResultVO(200,"修改成功");
        }catch (Exception e){
            return new ResultVO(300,"修改失败");
        }

    }
}
